import face_recognition
import cv2  # opencv-python
from ament_index_python.packages import get_package_share_directory # 获取share目录的绝对路径
import os

def main():
    face_image_path = os.path.join(get_package_share_directory('demo_python_service') ,'resource/face.jpg')
    print(f'图片的路径为：{face_image_path}')

    image = cv2.imread(face_image_path) # 读取图片
    image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB) # 转换为RGB格式

    face_locations = face_recognition.face_locations(image,number_of_times_to_upsample=1,model="hog")  # 检测人脸位置
    print(f"人脸个数为：{len(face_locations)}")
    # 绘制人脸框

    for top ,right,bottom,left in face_locations:
        cv2.rectangle(image,(left,top),(right,bottom),(255,0,0),4)


    # 显示图片
    cv2.imshow('face', cv2.cvtColor(image, cv2.COLOR_RGB2BGR))  # 转换回BGR格式以正确显示
    cv2.waitKey(0)